package com.voice.client.auth.handle;

import com.voice.client.auth.AuthHandler;
import com.wangjiacai.utils.utils.RandomStringUtils;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class DeviceAuthHandler implements AuthHandler {

    private static final String PARAM_RANDOM = "param-random";
    private static final String PARAM_SIGN = "param-sign";
    private static final String PARAM_SIGN_METHOD = "param-sign-method";
    private static final String HMACMD5 = "HMAC_MD5";

    private Map<String, Object> params = new HashMap();

    public DeviceAuthHandler(Map<String, String> authParams) {
        this.params.putAll(authParams);
        String random = RandomStringUtils.randomString(10);
        String deviceSecret =authParams.get("deviceSecret");
        String signOrigin = "deviceSecret=" + deviceSecret +"random=" + random;
        this.params.put(PARAM_RANDOM, random);
        this.params.put(PARAM_SIGN_METHOD, HMACMD5);
        String sign = (new HmacUtils(HmacAlgorithms.HMAC_MD5, deviceSecret)).hmacHex(signOrigin);
        this.params.put(PARAM_SIGN, sign);
        this.params.remove("deviceSecret");
    }

    public Map<String, Object> getAuthParams() {
        return this.params;
    }
}
